<?php
/* 用户表 */
namespace app\api\model;


use think\Db;
use think\Model;

class MessageBox extends Model {


    protected $tableName = 'message_box';
    
    const MESSAGE_STATUS_DRAFT=1;//存草稿箱
    const MESSAGE_STATUS_OK=2;//发布状态
    const MESSAGE_STATUS_DELETE=3;//删除
    
    const ARTICLE_TYPE=2;
    const ACTIVITY_TYPE=1;
    /*
     * 通过ID获取某条数据
     */
    public function getOneMessageBoxDataByID($id)
    {
        return Db::name($this->tableName)->where(['id'=>$id])->find();
    }
    /*
     * 插入单条数据
     */
    public  function insertMessageBox($data)
    {
        Db::name($this->tableName)->insert($data);
        return Db::name($this->tableName)->getLastInsID();
    }
    /*
     * 获取消息列表
     */
    public function getMessageBoxList($condition,$paginate=[],$orderBy=[]){
        return  Db::name($this->tableName)->where($condition)->order($orderBy)->paginate($paginate);
    }
    
    /*
     * 通过消息ID更新status
     */
    public function updateStatusByID($msgID,$status){
        if(!(int)$msgID) return false;
        return Db::name($this->tableName)->where(['id'=>$msgID])->update(['status'=>$status]);
    }
   
    /*
     * 通过消息ID更新数据
     */
    public function updateDataByID($msgID,$data){
        if(!(int)$msgID) return false;
        return Db::name($this->tableName)->where(['id'=>$msgID])->update($data);
    }
    
    /*
     * 添加消息点击量
     */
    public function setClickAmountIncByID($msgId){
        if (!(int)$msgId)return false;
        Db::name($this->tableName)->where(['id'=>$msgId])->setInc('click_amount');
    }
    
    /*
     * 添加消息转发量
     */
    public function setForwardAmountIncByID($msgId){
        if (!(int)$msgId)return false;
        Db::name($this->tableName)->where(['id'=>$msgId])->setInc('forward_amount');
    }
    /*
     * 获取消息状态
     */
    public function getStatusByID($msgId){
        if (!(int)$msgId)return false;
        $data=Db::name($this->tableName)->where(['id'=>$msgId])->field('status')->find();
        return $data['status'];
    }
}